package com.wenx.v3gateway.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

/**
 * 网关熔断降级控制器
 * 
 * @author wenx
 */
@Slf4j
@RestController
public class FallbackController {

    /**
     * 通用熔断降级处理
     */
    @RequestMapping("/fallback")
    public Mono<ResponseEntity<Map<String, Object>>> fallback(ServerWebExchange exchange) {
        String path = exchange.getRequest().getURI().getPath();
        String method = exchange.getRequest().getMethod().toString();
        
        log.warn("服务熔断降级触发 - 路径: {}, 方法: {}", path, method);
        
        Map<String, Object> result = new HashMap<>();
        result.put("code", 503);
        result.put("message", "服务暂时不可用，请稍后重试");
        result.put("data", null);
        result.put("timestamp", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        result.put("path", path);
        result.put("method", method);
        
        return Mono.just(ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).body(result));
    }

    /**
     * 认证服务熔断降级
     */
    @RequestMapping("/fallback/auth")
    public Mono<ResponseEntity<Map<String, Object>>> authFallback(ServerWebExchange exchange) {
        log.warn("认证服务熔断降级触发");
        
        Map<String, Object> result = new HashMap<>();
        result.put("code", 503);
        result.put("message", "认证服务暂时不可用，请稍后重试");
        result.put("data", null);
        result.put("timestamp", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        
        return Mono.just(ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).body(result));
    }

    /**
     * 系统服务熔断降级
     */
    @RequestMapping("/fallback/system")
    public Mono<ResponseEntity<Map<String, Object>>> systemFallback(ServerWebExchange exchange) {
        log.warn("系统服务熔断降级触发");
        
        Map<String, Object> result = new HashMap<>();
        result.put("code", 503);
        result.put("message", "系统服务暂时不可用，请稍后重试");
        result.put("data", null);
        result.put("timestamp", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        
        return Mono.just(ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).body(result));
    }

    /**
     * 批处理服务熔断降级
     */
    @RequestMapping("/fallback/batch")
    public Mono<ResponseEntity<Map<String, Object>>> batchFallback(ServerWebExchange exchange) {
        log.warn("批处理服务熔断降级触发");
        
        Map<String, Object> result = new HashMap<>();
        result.put("code", 503);
        result.put("message", "批处理服务暂时不可用，请稍后重试");
        result.put("data", null);
        result.put("timestamp", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        
        return Mono.just(ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).body(result));
    }

    /**
     * 存储服务熔断降级
     */
    @RequestMapping("/fallback/storage")
    public Mono<ResponseEntity<Map<String, Object>>> storageFallback(ServerWebExchange exchange) {
        log.warn("存储服务熔断降级触发");
        
        Map<String, Object> result = new HashMap<>();
        result.put("code", 503);
        result.put("message", "存储服务暂时不可用，请稍后重试");
        result.put("data", null);
        result.put("timestamp", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        
        return Mono.just(ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).body(result));
    }

    /**
     * 工作流服务熔断降级
     */
    @RequestMapping("/fallback/workflow")
    public Mono<ResponseEntity<Map<String, Object>>> workflowFallback(ServerWebExchange exchange) {
        log.warn("工作流服务熔断降级触发");
        
        Map<String, Object> result = new HashMap<>();
        result.put("code", 503);
        result.put("message", "工作流服务暂时不可用，请稍后重试");
        result.put("data", null);
        result.put("timestamp", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        
        return Mono.just(ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).body(result));
    }
}
